/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */

package com.android.music.download;

import java.io.IOException;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;

import com.android.music.MusicApplication;
import com.android.music.R;
import com.android.music.model.Music;
import com.android.music.util.FileOperate;
import com.android.music.util.Util;

public class LrcDownLoad {

    private FileOperate fileO = new FileOperate();

    private Context mContextDownLoad;

    private Intent intentMusicChangeBroadcast = new Intent(Util.MUSIC_CHANGE_BROADCAST);

    public void getAndSaveLrc(Context context, Music music) {
        mContextDownLoad = context;
        WebTask webTask = new WebTask(mContextDownLoad, music);
        webTask.execute();
    }

    private String getLrcUrl(Music music) {
        Object[] array = new Object[3];
        array[0] = music.title;
        array[1] = music.artist;
        if (music.artist == mContextDownLoad.getString(R.string.unknown_artist_name))
            array[1] = "";
        array[2] = Integer.toString(music.totalTime);
        // http://lrc.ttpod.com/q?title=%s&artist=%s&mediatype=%s&duration=%d&bitrate=%d&srate=%d&raw=%d&%s
        return String.format("http://lrc.ttpod.com/q?title=%s&artist=%s&duration=%s", array);
    }

    private String getTheLrcContent(String result) {
        if (result != null && result.indexOf("<content>") != -1)
            return result.substring(result.indexOf("<content>") + "<content>".length(), result
                    .indexOf("[10:00:00]"));
        return null;
    }

    class WebTask extends AsyncTask<String, Integer, Void> {

        private Context mContext;

        private Music mMusic;

        private String content;

        private HttpURLdownloader httpurldownloader;

        public WebTask(Context Context, Music music) {
            mContext = Context;
            mMusic = music;
            httpurldownloader = new HttpURLdownloader();
        }

        protected Void doInBackground(String... params) {
            String result;

            result = httpurldownloader.getTheStream(mContext, getLrcUrl(mMusic));
            try {
                if (getTheLrcContent(result) != null) {
                    fileO.addFileToSDCard(mMusic.path, getTheLrcContent(result));
                    this.content = result;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (content != null) {
                MusicApplication.getInstance().setLrcMap(mMusic);
                mContext.sendBroadcast(intentMusicChangeBroadcast);
                content = null;
            } else {
                Toast.makeText(mContext, R.string.cannot_find_lrc, Toast.LENGTH_SHORT).show();
                MusicApplication.getInstance().initLrc(mMusic.title);
                mContext.sendBroadcast(intentMusicChangeBroadcast);
            }
            // mContextDownLoad.stopService(null);
        }
    }
}
